package com.niuke;

import java.util.Scanner;
import java.util.Stack;

/**
 * @ClassName : HJ70 矩阵数据量统计
 * @Author : kele
 * @Date: 2023/3/28 22:05
 * @Description :
 */
public class MatrixCountNum {

    public static int solution(int[][] matrix, String str) {

        Stack<int[]> stack = new Stack<>();

        int index = 0;
        int res = 0;

        for (int i = 0; i < str.length(); i++) {

            char c = str.charAt(i);
            if (Character.isLetter(c)) {
                if (stack.isEmpty() || stack.peek() == null) {
                    stack.push(matrix[index]);
                } else {
                    int[] prev = stack.pop();
                    res += matrix[index][1] * prev[0] * prev[1];
                    stack.push(new int[]{prev[0], matrix[index][1]});
                }
                index++;
            } else if (c == '(') {
                stack.push(null);
            } else if (c == ')') {
                int[] cur = stack.pop();
                stack.pop();  //取出（

                if (stack.size() <= 1) {
                    break;
                } else if(stack.peek() != null){
                    int[] prev = stack.pop();
                    res += cur[1] * prev[0] * prev[1];
                    cur[0] = prev[0];
                }
                stack.push(cur);
            }
        }
        return res;
    }

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            String n = in.nextLine();
            int a[][] = new int[Integer.parseInt(n)][2];
            for (int[] ints : a) {
                String[] s = in.nextLine().split(" ");
                ints[0] = Integer.parseInt(s[0]);
                ints[1] = Integer.parseInt(s[1]);
            }
            String str = in.nextLine();

            System.out.println(solution(a, str));

        }
    }

}
